package com.lxyk.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxyk.mapper.AccountMapper;
import com.lxyk.pojo.Account;
import com.lxyk.service.AccountService;
import com.lxyk.utils.ConstantPropertiesUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
public class AccountServiceImpl implements AccountService {
    //
    @Autowired
    public AccountMapper accountMapper;
    @Override
    public boolean login(String userName, String userPwd) {
        log.info("userName="+userName+"userPwd="+userPwd);
        QueryWrapper<Account> qw = new QueryWrapper<>();
        qw.eq("user_name",userName);
        // 根据用户名查询对应的账号信息
        Account account = accountMapper.selectOne(qw);
        if (null == account) {
            return false;
        }
        // 对表单输入的密码进行MD5加密
        String encodePwd = DigestUtil.md5Hex(userPwd);
        // 比较数据库密码和加密的密码是否相等，条件成立：登录成功，否则登录失败
        if (Objects.equals(encodePwd,account.getUserPwd())) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 手机登录
     * @param phone   手机号
     * @param code    验证码
     * @return
     */
    @Override
    public boolean send(String phone, String code) {
        //判断手机号是否为空
        if (StringUtils.isEmpty(phone)){
            return false;
        }
        //整合阿里云短信服务
        //设置参数
        DefaultProfile profile = DefaultProfile.getProfile(
                ConstantPropertiesUtils.REGION_Id,
                ConstantPropertiesUtils.ACCESS_KEY_ID,
                ConstantPropertiesUtils.SECRECT);

        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        // 手机号
        request.putQueryParameter("PhoneNumbers",phone);
        // 签名名称
        request.putQueryParameter("SignName","阿里云短信测试");
        // 模板code
        request.putQueryParameter("TemplateCode","SMS_154950909");
        //验证码  使用json格式   {"code":"123456"}
        Map<String,Object> param = new HashMap();
        param.put("code",code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        // 调用方法进行短信发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 查询用户名
     * @param userName
     * @return
     */
    @Override
    public Account selectByName(String userName) {
        QueryWrapper<Account> qw = new QueryWrapper<>();
        qw.eq("user_name",userName);
        return accountMapper.selectOne(qw);
    }

    /**
           * 用户注册
     * @param userName   用户名
     * @param userPwd    密码
     * @param password   确认密码
     * @param userEmail  邮箱
     * @param userMobile 手机号
     * @return
             */
    @Override
    public int addAccount(String userName, String userPwd, String password, String userEmail, String userMobile) {
        //完成注册 ：封装用户对象并且保存
        Account account = new Account();
        account.setUserName(userName);
        account.setUserPwd(DigestUtil.md5Hex(userPwd));
        account.setUserEmail(userEmail);
        account.setUserMobile(userMobile);
        return accountMapper.insert(account);
    }

    @Override
    public boolean pwdUser(Account account1) {
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",account1.getUserName());
        int update = accountMapper.update(account1, wrapper);
        if (update > 0){
            return true;
        }else {
            return false;
        }
    }
}
